import { RouteComponent } from "vue-router";

type menuAlias = {
  // hidden: boolean
  alias: string;
}

export type MenuType = {
  label: string;
  name: string;
  hidden?: menuAlias
  component: Record<string, RouteComponent>;
};

export const genSubMenu = (menuList: MenuType[], prefix: string) => {
  const productSubMenu = menuList.map((val) => ({
    path: `/${prefix}/` + val.name,
    name: val.name,
    component: val.component,
  }));//

  const productDefault = menuList[0];

  productDefault &&
    productSubMenu.push({
      path: "",
      name: productDefault.name,
      component: productDefault.component,
    });

  return productSubMenu;
};
/**
 * 
 * @param list menu list string array
 * @param hash location hash striped unused string
 * @returns the index of hash in list, return -1 if not found
 */
type CalMenu = {
  name: string,
  hidden?: menuAlias
}
export const calMenuChosen = (list: CalMenu[], hash: string): number => {
  for (let i = 0, offset = 0; i < list.length; i++) {
    const cur = list[i]
    cur.hidden && offset++
    if (cur.name !== hash) continue;
    if (cur.hidden === undefined) return i - offset
    const alias = cur.hidden.alias;
    return calMenuChosen(list, alias)
  }
  return -1;
}